package com.xgst.runningmanager.viewmodel

import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.gym.baselib.base.BaseViewModel
import com.gym.baselib.utils.ProgressUtils
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.hjq.http.listener.OnUpdateListener
import com.xgst.runningmanager.bean.OrderBean
import com.xgst.runningmanager.bean.UpdateBean
import com.xgst.runningmanager.http.api.ErrorOrderAPI
import com.xgst.runningmanager.http.api.UpdateImageApi
import com.xgst.runningmanager.http.api.WorkOrderAPI
import okhttp3.Call
import java.io.File

class InputViewModel:BaseViewModel() {
    fun postFile(file:File,onResult:(String)->Unit){
        EasyHttp.post(this)
            .api(UpdateImageApi(file))
            .request(object : OnUpdateListener<UpdateBean> {
                override fun onUpdateStart(call: Call?) {
                    LogUtils.i("开始上传")
                    ProgressUtils.showProgress()
                }

                override fun onUpdateProgressChange(progress: Int) {
                    LogUtils.i("上传进度: $progress")
                }

                override fun onUpdateSuccess(result: UpdateBean) {
                    LogUtils.i("上传成功")
                    ProgressUtils.dismiss()
                    onResult(result.result)
                }

                override fun onUpdateFail(throwable: Throwable) {
                    LogUtils.i("上传失败",throwable.message)
                    ProgressUtils.dismiss()
                }

                override fun onUpdateEnd(call: Call?) {

                }
            })
    }

    fun submit(api:WorkOrderAPI){
        EasyHttp.post(this)
            .api(api)
            .request(object : OnHttpListener<OrderBean>{
                override fun onHttpSuccess(bean: OrderBean) {
                    ToastUtils.showShort("提交成功")
                    ActivityUtils.getTopActivity().finish()
                }

                override fun onHttpFail(throwable: Throwable) {
                    ToastUtils.showShort(throwable.message)
                }
            })
    }

    fun submitE(api: ErrorOrderAPI){
        EasyHttp.post(this)
            .api(api)
            .request(object : OnHttpListener<OrderBean>{
                override fun onHttpSuccess(bean: OrderBean) {
                    ToastUtils.showShort("提交成功")
                    ActivityUtils.getTopActivity().finish()
                }

                override fun onHttpFail(throwable: Throwable) {
                    ToastUtils.showShort(throwable.message)
                }
            })
    }
}